package com.jeesit.cloud.spot.business.impl.Impl;

import com.jeesit.cloud.dto.*;
import com.jeesit.cloud.model.*;
import com.jeesit.cloud.spot.business.impl.IBizOrgBiz;
import com.jeesit.cloud.spot.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Slf4j
public class BizOrgBizImpl implements IBizOrgBiz {

    @Autowired
    private IBizOrgService bizOrgService;
    @Autowired
    private IBizOrgAccountService bizOrgAccountService;


    /**
     * 添加业务部门和账户关系
     * @param user
     * @param id
     * @param entity
     * @return
     */
    @Override
    @Transactional
    public JSONResult<BizOrg> saveDataBizOrg(User user, Long id, BizOrgDto entity) {
        BizOrg it = new BizOrg();
        if(id==null){
            it.setDeleted(false);
            it.setAddTime(new Date());
            it.setAddUserUuid(user.getId());
            it.setAddUserName(user.getNickName());
        } else {
            it = bizOrgService.getById(entity.getUuid());
            it.setModTime(new Date());
            it.setModUserUuid(user.getId());
            it.setModUserName(user.getNickName());
        }
        it.setName(entity.getName());
        it.setFutureMatchType(entity.getFutureMatchType());
        it.setSpotMatchType(entity.getSpotMatchType());
        it.setOrgUuid(entity.getOrgUuid());
        it.setTreePath(entity.getTreePath());
        it.setBizOrgUuid(entity.getBizOrgUuid());
        it.setRemark(entity.getRemark());
        bizOrgService.saveOrUpdate(it);

        BizOrgAccountDto bizOrgAccountDto = new BizOrgAccountDto();
        bizOrgAccountDto.setStatsGroupUuid(entity.getUuid());
        List<BizOrgAccount> bizOrgAccountList = bizOrgAccountService.list(0,bizOrgAccountDto);

        for (BizOrgAccount bizOrgAccount : bizOrgAccountList) {
            bizOrgAccountService.removeById(bizOrgAccount.getUuid());
        }
        if(entity.getUserIds()!=null){
            for (int i = 0; i < entity.getUserIds().length; i++) {
                BizOrgAccount bizOrgAccount = new BizOrgAccount();
                bizOrgAccount.setDeleted(false);
                bizOrgAccount.setAddTime(new Date());
                bizOrgAccount.setAddUserUuid(user.getId());
                bizOrgAccount.setAddUserName(user.getNickName());
                bizOrgAccount.setStatsGroupUuid(it.getUuid());
                bizOrgAccount.setCtpUuid(entity.getUserIds()[i]);
                bizOrgAccount.setCtpUserCode(entity.getUserCodes()[i]);
                bizOrgAccountService.saveOrUpdate(bizOrgAccount);
            }
        }
        JSONResult<BizOrg> res = new JSONResult<>();
        res.setMessage("添加成功");
        res.setSuccess(true);
        res.setData(it);
        return res;
    }

    /**
     * 删除业务部门和账户
     * @param user
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public JSONResult<BizOrg> delBizOrg(User user, Long[] ids){
        for(int i=0;i<ids.length;i++){
            BizOrg it = bizOrgService.getById(ids[i]);
            bizOrgService.removeById(it.getUuid());
            BizOrgAccountDto bizOrgAccountDto = new BizOrgAccountDto();
            bizOrgAccountDto.setStatsGroupUuid(it.getUuid());
            List<BizOrgAccount> bizOrgAccountList = bizOrgAccountService.list(0,bizOrgAccountDto);
            for (BizOrgAccount bizOrgAccount : bizOrgAccountList) {
                bizOrgAccountService.removeById(bizOrgAccount.getUuid());
            }
        }
        return JSONResult.getSimpleResult(true,"删除成功");
    }
}
